package TAIC.text;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Vector;

public class Preprocess {
	public static void main ( String arg[]) throws Exception{
//		if ( arg.length < 1 ) {
//			System.out.println ( "please input the original dict name")  ;
//		}
//		Preprocess temp = new Preprocess ()  ;
//		String buff= "a's.d~\\./~^()" ;
//		System.out.println ( temp.nextToken ( buff))  ;
		//File f = new File ( "a.txt"); 
		//System.out.println (f.getAbsolutePath());
		//c.compare( str1, str2 ) ;
		(new Preprocess()).init ( "src\\TAIC\\text\\dict.txt" ) ;
	}

	public void init ( String filename ){
		try {
			Scanner scanner = new Scanner ( new FileInputStream ( filename )) ;
			while ( scanner.hasNextLine() ) {
				String buffer = scanner.nextLine() ;
				int pos = getWord ( buffer ) ;   
				String word = token ; // the header, i.e the English word
				if ( word.indexOf( " ") != -1 ) continue;
				if ( pos >= buffer.length() ) continue ; 
				
				int wordID = english.getID( word) ;
				buffer = buffer.substring( pos ) ;				
				Translation tr = new Translation () ;
				Vector <Translation> trs = dict.get( wordID ) ;  
				if ( trs == null ) {
					trs = new Vector < Translation > () ;
					dict.put( wordID ,  trs );
				}
				trs.add( tr ) ; 
				
				String meaning ; 
				pos = 0 ; 
				while ( (pos = nextToken ( buffer, pos ) ) != -1 ) {
					meaning= token ;
					int meanID = german.getID( meaning ) ;
					tr.add( new WordProb ( meanID, 0.0)) ; 
				}
				
				for ( WordProb wp : tr.vec ) wp.prob = 1.0 / tr.size() ;
			}
			english.save() ;
			german.save() ;
			out ( "Mapping.txt") ; 
		} catch ( Exception e ) { 
			e.printStackTrace() ; 
		}
		
	}

	public void out (String file ) {
		try {
			PrintStream fout = new PrintStream ( new FileOutputStream ( file )) ; 
			for ( Map.Entry < Integer, Vector < Translation> > item : dict.entrySet() ){
				fout.print ( item.getKey() ) ;
				int len = item.getValue().size() ; 
				HashMap < Integer, Double > prob = new HashMap < Integer, Double > () ;
				for ( Translation tr : item.getValue() ) {
					for ( int i = 0; i < tr.size() ;  i ++ ) {
						Double p ; 
						if ( (p = prob.get(tr.get(i).vec)) == null) {
							prob.put( tr.get(i).vec , tr.get(i).prob ) ; 							
						} 
						else prob.put( tr.get(i).vec, tr.get(i).prob + p ) ; 
					}
				}
				fout.print ( " " + prob.size() ) ;
				for ( Map.Entry<Integer, Double> m : prob.entrySet() ) fout.print ( " " + m.getKey() + " " + m.getValue()/len ) ;
				fout.print("\n") ;
			}				
		}catch ( Exception e ) {
			
		}
	}
	
	int getWord ( String buffer ){
		int i ;
		for ( i = 0; i < buffer.length() ; i ++ ) 
			if ( buffer.charAt( i ) == '\t') break;
		token = buffer.substring( 0, i ).trim(); 
		return  i + 1 ;
	}

	int nextToken ( String buffer, int pos ) {
		if ( buffer.length() == 0 ) return -1 ; 
		int xx,i ;
		for ( xx = pos ; xx < buffer.length() ; xx ++ ) 
			if ( buffer.charAt( xx ) == ' ' || buffer.charAt( xx ) == ',' || buffer.charAt( xx ) == ';'|| buffer.charAt( xx ) == '\t' ) continue ;
			else if ( buffer.charAt( xx ) == '(' || buffer.charAt( xx ) == '[') {
				for ( ; xx < buffer.length() ; xx ++ ) 
					if ( buffer.charAt( xx ) == ')' || buffer.charAt( xx ) == ']' ) break;
			}
			else break ; 
		
		if  ( xx >= buffer.length() ) return -1 ; 
		for ( i = xx ; i < buffer.length() ; i ++ ) 
			if ( buffer.charAt( i ) == ',' || buffer.charAt( i ) == ';' || buffer.charAt( i ) == '\t' || 
					(buffer.charAt( i ) == ' ') || (buffer.charAt( i ) == '(') || (buffer.charAt( i ) == '[')) break;
		token = LanguageVec.normallize( buffer.substring( xx, i ) ) ; 
		return i ; 
		
	}
	
	String token ; 
	static LanguageVec english = new LanguageVec ( "english.txt") ; 
	static LanguageVec german = new LanguageVec ( "german.txt")  ;
	HashMap < Integer, Vector < Translation> > dict = new HashMap < Integer , Vector < Translation > > () ;  
}
